// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { NotificationCenter } from "../utils/NotificationCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MsgBoxLogic extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        NotificationCenter.Instance.on("close_msgbox", this.clickClose, this);
    }
    
    onDestroy() {
        NotificationCenter.Instance.off("close_msgbox", this.clickClose, this);
        if (cc.sys.WECHAT_GAME == cc.sys.platform) {
            let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
            logicCenter.bannerAd.hide();
        }

        // let resList = cc.loader.getDependsRecursively('prefab/msgBox');
        // var textures = [];
		// for (var i = 0; i < resList.length; ++i) {
		//     var item = cc.loader.getRes(resList[i]);
		//     if (item instanceof cc.Texture2D) {
		//         textures.push(item);
		//     }
        // }
    }

    start () {

    }

    // update (dt) {}

    public setupView(content: string, type: number, 
            confirm: cc.Component.EventHandler = null,
            cancel: cc.Component.EventHandler = null,
            share: cc.Component.EventHandler = null,
            title: string = null,
            iconPath: string = null) {
        let label_title = cc.find('im_bg/label_title', this.node);
        if (title != null && title != undefined) {
            label_title.getComponent(cc.Label).string = title;
        }
        let label_content = cc.find('im_bg/label_content', this.node);
        label_content.getComponent(cc.Label).string = content || '';
        
        let im_icon = cc.find('im_bg/im_icon', this.node);
        if (iconPath == null || iconPath == undefined) {
            im_icon.active = false;
        }else{
            cc.loader.loadRes(iconPath, cc.SpriteFrame, function (err, spriteFrame) {
                im_icon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                im_icon.active = true;
                label_content.y = 90;
            });
        }

        let bt_video = cc.find('im_bg/btn_ad', this.node);
        bt_video.active = (type == 1);
        bt_video.getComponent(cc.Button).clickEvents.push(confirm);
        
        let bt_share = cc.find('im_bg/btn_share', this.node);
        bt_share.active = (type == 2);
        bt_share.getComponent(cc.Button).clickEvents.push(share);

        let bt_confirm = cc.find('im_bg/btn_confirm', this.node);
        bt_confirm.active = (type == 3);
        bt_confirm.getComponent(cc.Button).clickEvents.push(confirm);

        let bt_close = cc.find('im_bg/btn_close', this.node);
        if (cancel != null && cancel != undefined) {
            bt_close.getComponent(cc.Button).clickEvents.push(cancel);
        }

        if (cc.sys.WECHAT_GAME == cc.sys.platform) {

            let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
            logicCenter.bannerAd
                    .show()
                    .then(() => {
                        console.log("show ok");
                    })
                    .catch(res => {
                        console.log("show error", res);
                    });
        }
    }

    public clickClose() {
        this.node.active = false;
        this.node.destroy();
    }


}
